package com.haier.controller.h5;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.haier.service.IHpAreaGoCountService;
import com.haier.service.IhaierService;
import com.haier.utils.EmptyUtils;
import com.haier.vo.ResponseVo;

@RestController
@RequestMapping(value = "/hipassjava/h5/go")
public class H5GoContoller {
	private static final Logger logger = LoggerFactory.getLogger(H5GoContoller.class);
	@Autowired
	private IHpAreaGoCountService iHpAreaGoCountService;
	@Autowired
	private IhaierService ihaierService;

	/**
	 * 我的go
	 */
	@RequestMapping(value = "/myGo", method = RequestMethod.POST)
	public Object myGo(@RequestBody JSONObject params) {
		try {
			String jobNumber = params.getString("jobNumber");
			if (EmptyUtils.isEmpty(jobNumber)) {
				return ResponseVo.checkError("工号不能为空");
			}
			String name = params.getString("name");
			if (EmptyUtils.isEmpty(name)) {
				return ResponseVo.checkError("名称不能为空");
			}
			String areaCode = params.getString("areaCode");
			if (EmptyUtils.isEmpty(areaCode)) {
				return ResponseVo.checkError("区域code不能为空");
			}
			return iHpAreaGoCountService.myGo(params);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 获取url
	 */
	@RequestMapping(value = "/myUrl", method = RequestMethod.POST)
	public Object myUrl(@RequestBody JSONObject params) {
		try {
			String jobNumber = params.getString("jobNumber");
			if (EmptyUtils.isEmpty(jobNumber)) {
				return ResponseVo.checkError("工号不能为空");
			}
			Integer validTime = params.getInteger("validTime");
			if (EmptyUtils.isEmpty(validTime)) {
				return ResponseVo.checkError("validTime不能为空");
			}
			Integer entryTimes = params.getInteger("entryTimes");
			if (EmptyUtils.isEmpty(entryTimes)) {
				return ResponseVo.checkError("entryTimes不能为空");
			}

			JSONObject result = new JSONObject();
			String url = ihaierService.getQr(jobNumber, validTime, entryTimes);
			result.put("url", url);
			return ResponseVo.success(result);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}
}
